package com.woniuxy.kaer.service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.kaer.commons.util.Data;
import com.woniuxy.kaer.service.entity.Consumables;
import com.woniuxy.kaer.service.entity.ServiceBase;
import com.woniuxy.kaer.service.entity.ServiceProject;
import com.woniuxy.kaer.service.mapper.ConsumablesMapper;
import com.woniuxy.kaer.service.mapper.ServiceBaseMapper;
import com.woniuxy.kaer.service.mapper.ServiceProjectMapper;
import com.woniuxy.kaer.service.param.AddServiceProjectParam;
import com.woniuxy.kaer.service.service.ServiceProjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 程诗洋
 * @since 2025年2月17日
 */
@Service
public class ServiceProjectServiceImpl extends ServiceImpl<ServiceProjectMapper, ServiceProject> implements ServiceProjectService {

    @Resource
    private ServiceProjectMapper serviceProjectMapper;
    @Resource
    private ServiceBaseMapper serviceBaseMapper;
    @Resource
    private ConsumablesMapper consumablesMapper;
    @Override
    public void addServiceProject(AddServiceProjectParam param) throws Exception {
//        ServiceBase serviceBase = serviceBaseMapper.selectById(param.getServiceBaseId());
        //ServiceBase serviceBase = serviceBaseMapper.selectById(param.getServiceBaseId());
//        if(null==serviceBase){
//            throw new Exception("服务类别不存在");
//        }
//        if(serviceBase.getServiceBaseState()!= Data.SERVICE_BASE_STATE_NORMAL){
//            throw new Exception("服务类别状态异常");
//        }
        ServiceProject serviceProject = BeanUtil.toBean(param, ServiceProject.class);
        serviceProjectMapper.insert(serviceProject);
    }

    @Override
    public List<ServiceProject> getServiceProjectByServiceBaseId(Integer serviceBaseId) throws Exception {
        QueryWrapper<ServiceProject> serviceProjectQueryWrapper = new QueryWrapper<>();
        serviceProjectQueryWrapper.eq("service_base_id",serviceBaseId);
        List<ServiceProject> serviceProjects = serviceProjectMapper.selectList(serviceProjectQueryWrapper);
        return serviceProjects;
    }

    @Override
    public void removeServiceProjectById(Integer serviceProjectId) throws Exception {
        ServiceProject serviceProject = serviceProjectMapper.selectById(serviceProjectId);
        if(null==serviceProject){
            throw new Exception("服务项目不存在");
        }
        QueryWrapper<Consumables> consumablesQueryWrapper = new QueryWrapper<>();
        consumablesQueryWrapper.eq("service_project_id",serviceProjectId);
        List<Consumables> consumablesList = consumablesMapper.selectList(consumablesQueryWrapper);
        if(consumablesList!=null&&consumablesList.size()!=0){
            for (Consumables consumables : consumablesList) {
                consumablesMapper.deleteById(consumables.getConsumablesId());
            }
        }
        serviceProjectMapper.deleteById(serviceProjectId);
    }
}
